﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZWZ.Web.WebAdmin.Update
{
    public partial class UpdateUser : System.Web.UI.Page
    {
        protected string UserID
        {
            get
            {
                if (ViewState["UserID"] == null)
                {
                    if (string.IsNullOrEmpty(Request["uid"]))
                    {
                        ViewState["UserID"] = Codes.IDHelper.CreateID_UserInfo();
                    }
                    else
                    {
                        ViewState["UserID"] = Request["uid"];
                    }
                }
                return ViewState["UserID"].ToString();
            }
        }
        private string GROUPTYPE = ZWZ.Enums.User.GroupType.用户.ToString();
        private const string IMAGEFILEDIRID = "FD1";
        private const string DEFAULTIMGURL = "/webadmin/images/nopic.jpg";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定用户组
                DdlUserGroup.DataSource = DBProvider.DB.Ctx.User_Group.Where(i => i.Type == GROUPTYPE);
                DdlUserGroup.DataValueField = "ID";
                DdlUserGroup.DataTextField = "Name";
                DdlUserGroup.DataBind();
                DdlGender.BindEnumDataSource(typeof(ZWZ.Enums.User.Gender));
                DdlStatus.BindEnumDataSource(typeof(ZWZ.Enums.User.UserStatus));
                Image1.ImageUrl = DEFAULTIMGURL;
                //绑定用户
                var userinfo = DBProvider.DB.Ctx.User_Info.Where(user => user.ID == UserID).FirstOrDefault();
                if (userinfo != null)
                {
                    Image1.ImageUrl = string.IsNullOrEmpty(userinfo.PicUrl) ? DEFAULTIMGURL : userinfo.PicUrl;
                    TxtNickName.Text = userinfo.NickName;
                    DdlUserGroup.SelectedValue = userinfo.GroupID;
                    DdlGender.SelectedValue = userinfo.Gender;
                    DdlStatus.SelectedValue = userinfo.Status;
                    TxtTel.Text = userinfo.Tel;
                    TxtEmail.Text = userinfo.Email;
                    TxtBirthday.Text = userinfo.Birthday.ToString("yyyy-MM-dd");
                    TxtIntroduct.Text = userinfo.Introduction;
                }
            }
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            try
            {
                ModelBase.User_Info userinfo;
                if (string.IsNullOrEmpty(Request["uid"]))
                {
                    if (string.IsNullOrEmpty(TxtPassword.Text)) throw new Exception(" 请输入密码！");
                    userinfo = new ModelBase.User_Info();
                    userinfo.ID = UserID;
                    userinfo.GroupID = DdlUserGroup.SelectedValue;
                    userinfo.GroupName = DdlUserGroup.SelectedItem.Text;
                    userinfo.GroupType = GROUPTYPE;
                    userinfo.NickName = TxtNickName.Text;
                    userinfo.PicUrl = Image1.ImageUrl;
                    userinfo.Tel = TxtTel.Text;
                    userinfo.Status = DdlStatus.SelectedValue;
                    userinfo.Gender = DdlGender.SelectedValue;
                    userinfo.Email = TxtEmail.Text;
                    userinfo.Birthday = Convert.ToDateTime(TxtBirthday.Text);
                    userinfo.Introduction = TxtIntroduct.Text;
                    userinfo.Password = Codes.Md5Helper.GetKeyPasswordString(TxtPassword.Text);
                    userinfo.CreateDate = DateTime.Now;
                    DBProvider.DB.Ctx.User_Info.AddObject(userinfo);
                    DBProvider.DB.Ctx.SaveChanges();
                }
                else
                {
                    userinfo = DBProvider.DB.Ctx.User_Info.Where(user => user.ID == UserID).FirstOrDefault();
                    userinfo.GroupID = DdlUserGroup.SelectedValue;
                    userinfo.GroupName = DdlUserGroup.SelectedItem.Text;
                    userinfo.GroupType = GROUPTYPE;
                    userinfo.NickName = TxtNickName.Text;
                    userinfo.PicUrl = Image1.ImageUrl;
                    userinfo.Tel = TxtTel.Text;
                    userinfo.Status = DdlStatus.SelectedValue;
                    userinfo.Gender = DdlGender.SelectedValue;
                    userinfo.Email = TxtEmail.Text;
                    userinfo.Birthday = Convert.ToDateTime(TxtBirthday.Text);
                    userinfo.Introduction = TxtIntroduct.Text;
                    if (!string.IsNullOrEmpty(TxtPassword.Text)) { userinfo.Password = Codes.Md5Helper.GetKeyPasswordString(TxtPassword.Text); }
                    DBProvider.DB.Ctx.SaveChanges();
                }
                new Codes.MessageBox(this).Show("保存成功！").RefrashOpener().CloseWindow().Send();
            }
            catch (Exception exp)
            {
                new Codes.MessageBox(this).Show("输入错误：" + exp.Message).Send();
            }
        }
        protected void BtnUpload_Click(object sender, EventArgs e)
        {
            try
            {
                if (!FileUpload1.HasFile) { throw new Exception("请选择文件！"); }
                Image1.ImageUrl = Codes.UploadFileHelper.UpLoadFile(IMAGEFILEDIRID, UserID, FileUpload1.PostedFile, Codes.UpLoadFileType.Image);
            }
            catch (Exception exp)
            {
                new Codes.MessageBox(this).Show(exp.Message).Send();
            }
        }
    }
}